﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUIDialogs
{
    public partial class BaseDialog : Form
    {
        public BaseDialog()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Method to be overridden in derived classes.
        /// Determine is the manditory data is correct on the form
        /// </summary>
        /// <returns>True if the data is correct, false if otherwise</returns>
        protected virtual bool IsValid()
        {
            return false;
        }

        protected virtual void UpdateData()
        {
        }

        /// <summary>
        /// If the OK button was pressed and the manditory data is bad, prevent the form from closing.
        /// </summary>
        /// <param name="sender">The closing form</param>
        /// <param name="e">The arguments of the event</param>
        private void BaseDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.OK && !IsValid())
            {
                e.Cancel = true;
                return;
            }

            if (DialogResult == DialogResult.OK)
                UpdateData();
        }
    }
}
